﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace SimpleText.Framework
{

	/// <summary>
	/// The engine for use on the site.
	/// </summary>
	/// <remarks>
	/// Basically a lighter and simpler version of LanguageVersioningModel. This class is intended for use on the language versioned site, and therefor only exposes the texts, and not comments, author info etc.
	/// A text engine only operates in the context of a single text group at a time
	/// </remarks>
	public class TextEngine
	{
		protected LanguageVersioningModel Model { get; set; }
		protected string LanguageShortname { get; set; }

		public MissingTextBehaviours MissingTextBehaviour = MissingTextBehaviours.ReturnWarning;

		public TextEngine(string xml)
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(xml);

			InitTextEngine(xmlDoc);
		}
		public TextEngine(XmlDocument xmlDoc)
		{
			InitTextEngine(xmlDoc);
		}

		protected void InitTextEngine(XmlDocument xmlDoc)
		{
			Model = new LanguageVersioningModel(xmlDoc);
		}

		public void SetLanguageByShortname(string languageShortname)
		{
			LanguageShortname = languageShortname;
		}

		public string GetWeb(string textDefinitionName)
		{
			return System.Web.HttpUtility.HtmlEncode(Get(textDefinitionName)).Replace("\r\n", "<br />").Replace("\n", "<br />");
		}
		public string GetJscript(string textDefinitionName)
		{
			return Get(textDefinitionName).Replace("'", "\\'").Replace("\"", "\\\"").Replace("\r\n", "\\r\\n").Replace("\n", "\\n");
		}

		public string Get(string textDefinitionName)
		{
			if (LanguageShortname == null)
			{
				throw new ApplicationException("No language short name set (call SetLanguageByShortname(...) after initializing the TextEngine object)");
			}
			string foundTextContent = null;
			TextDefinition foundTextDef = null;
			if (Model.TextDefinitions.ContainsKey(textDefinitionName))
			{
				foundTextDef = Model.TextDefinitions[textDefinitionName];
			}
			Text foundText = null;
			if (foundTextDef != null)
			{
				foundText = foundTextDef[LanguageShortname];
			}

			bool textIsMissing = false;

			if (foundText == null)
			{
				textIsMissing = true;
			}
			else if (String.IsNullOrEmpty(foundText.Content))
			{
				textIsMissing = true;
			}
			if (textIsMissing)
			{
				switch (MissingTextBehaviour)
				{
					case MissingTextBehaviours.Ignore:
						foundTextContent = "";
						break;
					case MissingTextBehaviours.ReturnWarning:
						foundTextContent = "#" + textDefinitionName + "_" + LanguageShortname + "#";
						break;
					case MissingTextBehaviours.Abort:
						throw new ApplicationException("No text with definition name \"" + textDefinitionName + "\" and language short name " + LanguageShortname + " found among the loaded texts");
				}
			}
			else
			{
				foundTextContent = foundText.Content;
			}

			return foundTextContent;

		}
	}

}